package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.ReqQryQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/29.
 */
public class CITIC02QryQuotaInfo extends CITIC02Base implements IQuotaInfo {

    @Override
    public RetAtsQuotaInfo queryQuotaInfo(ReqQryQuotaInfo reqQryQuotaInfo) throws Exception {
        RetAtsQuotaInfo retAts = new RetAtsQuotaInfo(reqQryQuotaInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            CITIC02Header header = new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCLCRTQ";
            //            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            CPCUSTCD	企业端经销商客户代码	VARCHAR	20	Y
            xmlWriterBody.writeElementString("CPCUSTCD", reqQryQuotaInfo.DealerCode);
//            BKCUSTCD	银行端借款企业ID	VARCHAR	20	N
            xmlWriterBody.writeElementString("BKCUSTCD", "");
//            LONCPNM	借款企业名称	VARCHAR	100	Y
            xmlWriterBody.writeElementString("LONCPNM", reqQryQuotaInfo.DealerName);
//            OPBRHCD	经办分行编号	VARCHAR	10	N
            xmlWriterBody.writeElementString("OPBRHCD", "");
//            OPBRHNM	经办分行名称	VARCHAR	100	N
            xmlWriterBody.writeElementString("OPBRHNM", "");
//            CREDITNO	额度编号	VARCHAR	20	N
            xmlWriterBody.writeElementString("CREDITNO", "");
//            FIELD1	属性1	VARCHAR	30	N
            xmlWriterBody.writeElementString("FIELD1", "");
//            FIELD2	属性2	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2", "");
//            FIELD3	属性3	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD3", "");
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(), header);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/stream/RETCODE");
                    if ("AAAAAAA".equals(tmpRetCode)) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        while (tmpBkReader.read("/stream/list/row")) {
                            RetAtsQuotaInfoDetail tmpRetDetail = new RetAtsQuotaInfoDetail();
//                            CPCUSTCD	企业端经销商客户代码	VARCHAR	20	Y
                            tmpRetDetail.DealerCode = reqQryQuotaInfo.DealerCode;
//                            TOTCRD	总额度	VARCHAR	19,2	N
                            tmpRetDetail.Limit = tmpBkReader.readedXElement.getSingleText("TOTCRD");
//                            ALDCRD	可用额度	VARCHAR	19,2	N
                            tmpRetDetail.UnuseLimit = tmpBkReader.readedXElement.getSingleText("ALDCRD");
//                            CRDSTDT	额度合同开始日期	DATE		Y
                            tmpRetDetail.LimitBeginDate = tmpBkReader.readedXElement.getSingleText("CRDSTDT");
//                            CRDEDDT	额度合同结束日期	DATE		Y
                            tmpRetDetail.LimitEndDate = tmpBkReader.readedXElement.getSingleText("CRDEDDT");
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetCode;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
